/* ****************************************************************************
hrtime.h
high resolution time
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */

#ifndef SPEARS_SPEARS_HPTIME
#define SPEARS_SPEARS_HPTIME

#include "../my-prerequisites.h"
#include "../../mathinc/mathinc.h"

#	if defined(WIN32) & WIN32
#		include <windows.h>
#	endif

namespace Spears
{
	class HRTime
	{
	public:
		HRTime()
		{
#if defined(WIN32) & WIN32
			QueryPerformanceFrequency(&m_freq);
#endif
		}

		HRTime(const HRTime& rhs)
#if defined(WIN32) & WIN32
			: m_freq(rhs.m_freq)
			, m_beg_time(rhs.m_beg_time)
			, m_end_time(rhs.m_end_time)
#endif
		{}

		HRTime(HRTime&& rhs)
#if defined(WIN32) & WIN32
			: m_freq(std::move(rhs.m_freq))
			, m_beg_time(std::move(rhs.m_beg_time))
			, m_end_time(std::move(rhs.m_end_time))
#endif
		{}

		virtual ~HRTime()
		{}

		HRTime& operator=(const HRTime& rhs)
		{
#if defined(WIN32) & WIN32
			m_freq = rhs.m_freq;
			m_beg_time = rhs.m_beg_time;
			m_end_time = rhs.m_end_time;
#endif
		}

		HRTime& operator=(HRTime&& rhs)
		{
#if defined(WIN32) & WIN32
			m_freq = std::move(rhs.m_freq);
			m_beg_time = std::move(rhs.m_beg_time);
			m_end_time = std::move(rhs.m_end_time);
#endif
		}

	public:
		void start()
		{
#if defined(WIN32) & WIN32
			QueryPerformanceCounter(&m_beg_time);
#endif
		}

		/*
		@retval: time elapsed from start() in millisecond.
		*/
		void stop()
		{
#if defined(WIN32) & WIN32
			QueryPerformanceCounter(&m_end_time);
#endif
		}

		Math::decimal elapsed_milliseconds()
		{
#if defined(WIN32) & WIN32
			Math::decimal elapsed = (Math::decimal)(m_end_time.QuadPart - m_beg_time.QuadPart)
				* 1000.f / (Math::decimal)m_freq.QuadPart;
			return std::move(elapsed);
#else
			return 0.0f;
#endif
		}

		Math::decimal elapsed_seconds()
		{
#if defined(WIN32) & WIN32
			Math::decimal elapsed = (Math::decimal)(m_end_time.QuadPart - m_beg_time.QuadPart)
				/ (Math::decimal)m_freq.QuadPart;
			return std::move(elapsed);
#else
			return 0.0f;
#endif
		}

	private:
#if defined(WIN32) & WIN32
		LARGE_INTEGER	m_freq;
		LARGE_INTEGER	m_beg_time;
		LARGE_INTEGER	m_end_time;
#endif
	};
}

#endif